package com.liliya.servlet.pro;

import com.liliya.pojo.Cate;
import com.liliya.pojo.Pro;
import com.liliya.pojo.SubCate;
import com.liliya.service.ProService;
import com.liliya.service.SubCateService;
import com.liliya.service.impl.CateServiceImpl;
import com.liliya.service.impl.ProServiceImpl;
import com.liliya.service.impl.SubCateServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/13 18:57
 */
@WebServlet(urlPatterns = "/pro/add")
public class ProAddServlet extends HttpServlet {
    private CateServiceImpl cateService = new CateServiceImpl();
    private SubCateService subCateService = new SubCateServiceImpl();
    private ProService proService = new ProServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Cate> cates = cateService.findCate(1, cateService.totalCate());
        //先把第一个的大类下面的子类渲染上去
        List<SubCate> subcates = subCateService.findSubCate(1, subCateService.totalSubCate(), cates.get(0).getCatenum());
        req.setAttribute("cates",cates);
        req.setAttribute("subcates",subcates);
        req.getRequestDispatcher("./proadd.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            System.out.println(entry.getKey()+":"+entry.getValue()[0]);
        }
        Pro pro = new Pro();
        pro.setProname(req.getParameter("proname"));
        pro.setCatenum(Integer.parseInt(req.getParameter("catenum")));
        pro.setSubcatenum(Integer.parseInt(req.getParameter("subcatenum")));
        pro.setPropublisher(req.getParameter("propublisher"));
        pro.setProauth(req.getParameter("proauth"));
        pro.setProdes(req.getParameter("prodes"));
        pro.setProprice(Integer.parseInt(req.getParameter("proprice")));
        pro.setProdisprice(Integer.parseInt(req.getParameter("prodisprice")));
        pro.setProrecom(req.getParameter("prorecom").equals("t")?1:0);
        pro.setPridiscount(req.getParameter("pridiscount").equals("t")?1:0);
        pro.setProsimgname(req.getParameter("prosimgname"));
        pro.setProlimgname(req.getParameter("prolimgname"));
        pro.setProstock(Integer.parseInt(req.getParameter("prostock")));
        pro.setProsalenum(Integer.parseInt(req.getParameter("prosalenum")));
        pro.setProaccessnum(Integer.parseInt(req.getParameter("proaccessnum")));
        pro.setProweight(Integer.parseInt(req.getParameter("proweight")));

        proService.insertPro(pro);


        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write("{msg:我觉得还行}");
    }
}
